package LCR;

public class addBinary {
    public static void main(String[] args) {
        System.out.println(addBinary2("1010", "1011"));
    }

    static public String addBinary2(String a, String b) {
        //位运算
        int x = Integer.parseInt(a,2);
        int y = Integer.parseInt(b,2);

        int answer = 0;
        int carry = 0;

        while(y != 0) {
            answer = x ^ y;
            carry = (x & y)<< 1;
            x =answer;
            y = carry;
        }
        return Integer.toBinaryString(x);

    }

    public static String addBinary(String a, String b) {
        StringBuffer sa = reserve(a);
        StringBuffer sb = reserve(b);
        StringBuffer ret = new StringBuffer();
        int numa = 0;
        int numb = 0;
        int flg = 0;
        while (numa < sa.length() && numb < sb.length()) {
            if (flg == 1) {
                if (sa.charAt(numa) == '1' && sb.charAt(numb) == '1') {
                    ret.append("1");
                    flg = 1;
                }else if(sa.charAt(numa) == '1' || sb.charAt(numb) == '1'){
                    ret.append("0");
                    flg = 1;
                }else {
                    ret.append("1");
                    flg = 0;
                }

            } else {
                if (sa.charAt(numa) == '1' && sb.charAt(numb) == '1') {
                    ret.append("0");
                    flg = 1;

                } else if (sa.charAt(numa) == '0' && sb.charAt(numb) == '0') {
                    ret.append("0");
                    flg = 0;
                } else {
                    ret.append("1");
                    flg = 0;
                }
            }

            numa++;
            numb++;
        }

        if (numa == sa.length()) {
            while (numb < sb.length()) {
                if (flg == 1 && sb.charAt(numb) == '1') {
                    flg = 1;
                    ret.append("0");
                } else if (flg == 1 && sb.charAt(numb) == '0') {
                    ret.append("1");
                    flg = 0;
                } else {
                    ret.append(sb.charAt(numb));
                }
                numb++;
            }
        }
        if (numb == sb.length()) {
            while (numa < sa.length()) {
                if (flg == 1 && sa.charAt(numa) == '1') {
                    flg = 1;
                    ret.append("0");
                } else if (flg == 1 && sa.charAt(numa) == '0') {
                    ret.append("1");
                    flg = 0;
                } else {
                    ret.append(sa.charAt(numa));
                }
                numa++;
            }
        }
        if (flg == 1) {
            ret.append("1");
        }
        return ret.reverse().toString();

    }

    static StringBuffer reserve(String a) {
        StringBuffer ret = new StringBuffer();
        for (int i = a.length() - 1; i >= 0; i--) {
            ret.append(a.charAt(i));
        }
        return ret;
    }
}
